package com.jsshse.controller.report;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

 
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.jsshse.service.report.RParamServiceI;
import com.jsshse.util.Pages;


@Controller
@RequestMapping("/report/rParam/*")
public class RParamController {
	

	@Resource
	private RParamServiceI rParamService;
	
	private static Logger log = Logger.getLogger(RParamController.class);
	
	@RequestMapping(value = "/save")
	public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String data = request.getParameter("data");
		log.debug(data);
		JSONObject json = JSONObject.fromObject(data);
		HashMap m= (HashMap)JSONObject.toBean(json,HashMap.class);
		
		String id=m.get("p_id").toString();
		log.debug(id);
		if(id.length()>0){
			rParamService.update(m);
		}else{
			rParamService.add(m);
		}

		
		JSONObject jsonObject = JSONObject.fromObject(m);      
		response.getWriter().write(jsonObject.toString());
		return null;
	}
	
	@RequestMapping("/del")
	public ModelAndView romove (HttpServletRequest request, HttpServletResponse response) throws Exception {
		 String idStr = request.getParameter("id")==null?"":request.getParameter("id"); 
		 log.debug("id:"+idStr); 
		 if(idStr.length()>0){
			 String[] ids = idStr.split(",");
			    for (int i = 0, l = ids.length; i < l; i++)
			    {
			        rParamService.delete(ids[i]);
			    }  
		 } 
		return null;
	}
	
	@RequestMapping(value = "/get", method = RequestMethod.POST)
	public ModelAndView get(@RequestParam("id") String id,HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map map = rParamService.queryById(id);
		JSONObject jsonObject = JSONObject.fromObject(map); 
		log.debug(jsonObject.toString()); 
		response.getWriter().write(jsonObject.toString());
		return null;
	}
	
	@RequestMapping("/list")
	public ModelAndView listAll (HttpServletRequest request, HttpServletResponse response) throws Exception {
		 String key = request.getParameter("key")==null?"":request.getParameter("key");        
 
	    int pageIndex = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("rows"));        
		String sortField = request.getParameter("sort");
		String sortOrder = request.getParameter("order");

	    StringBuffer sb=new StringBuffer();
	    Pages pages=new Pages();
	    
	    if(key.length()>0){
	    	sb.append(" AND NAME LIKE '%"+key+"%'");
	    }
    
	    pages.setSortField(sortField);
	    pages.setSortOrder(sortOrder);
	    pages.setQueryCondition(sb.toString());
	    pages.setPageSize(pageSize);
	    pages.setPageStart((pageIndex-1)*pageSize);
	    
	    pages = rParamService.queryForList(pages);
	    
	   HashMap result=new HashMap();
	    result.put("rows",pages.getDataList());
        result.put("total",pages.getTotal());
       
        JSONObject jsonObject = JSONObject.fromObject(result);      
        response.getWriter().write(jsonObject.toString());
		return null;
	}
 
}
